/*
 * 客户端监控项实现类。
 * Copyright (C) 2009  Xubin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sysmo.client.item;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import org.sysmo.communication.CommunicationInterface;

/**
 *监控项类主要运行两个脚本, 第一个数据采集脚本, 通过TimerTask定时执行采集数据的任务,
 * 每次采集数据后, 都要运行数据验证脚本, 判断是否产生Warning, 如果有, 将Warning添加
 * 到需要传给服务器的xml文件, 然后传送一起传送给服务器. 如果服务器暂时中断, 则将采集
 * 到的数据暂时存放到本地临时文件中, 等到服务器恢复连接后再传送给服务器.
 * @author Xubin
 */
public class Item {
    private String dataCollectScript;
    private String commandLine;
    private String dataTestScript;
    private Integer ItemID;
    private long period;
    private CommunicationInterface communication;
    private File localDataBuffer;

    private TimerTask task;

    /**
     * 建立新的Item实例, 由客户端传入ItemID和通讯接口实例,
     * @param id 客户端中监控的每个项都有一个ID,便于管理
     * @param comm 客户端传给Item的通讯接口, Item通过通讯接口将采集到的数据和警告
     * 发送给服务器, 以及从服务器下载数据采集脚本和数据验证脚本, 并保存在本地.
     */
    public Item(Integer id, CommunicationInterface comm) {
        /*...*/
        communication = comm;
        ItemID = id;
    }

    /**
     *建立Item时要调用start()方法, 创建一个定时任务, 以period为周期定时采集数据, 然
     * 后验证数据
     */
    public void start(){
        task = new TimerTask() {
            public void run() {
                doDataCollectScript(dataCollectScript, commandLine);
                doDataTestScript(dataTestScript);
            }
        };
        Timer t = new Timer();
        t.schedule(task, 0, period);
    }

    /**
     *客户端获取服务器消息需要删除一个监控项时调用delete()方法
     */
    public void delete(){

    }

    /**
     * 让某个监控项暂停采集时客户端调用pause()方法
     */
    public void pause(){

    }

    /**
     * 取消暂停时调用recover()方法
     */
    public void recover(){

    }

    /**
     * 当服务器需要改变某个监控项采集数据的周期时调用setPeriod()方法
     * @param itemperiod 客户端传入的需要更改的采集周期
     */
    public void setPeriod(long itemperiod){
        period = itemperiod;
    }

    private void doDataCollectScript(String dataCollectScript, String commandLine){

    }

    private void doDataTestScript(String dataTestScript){

    }

    private void sendToServer(String dataFile, CommunicationInterface comm){

    }

    private void saveToLocalBuffer(String dataFile){

    }

}
